package com.cgbl.cg.view

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import com.cgbl.cg.R
import com.lxj.xpopup.core.BottomPopupView
import com.robot.base.util.ToastUtils
import com.robot.base.view.CgTextView
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout

class AddTagPop(context: Context, val list: ArrayList<String>) : BottomPopupView(context) {
    private var confirmClickListener: (id: ArrayList<String>) -> Unit = {}

    private lateinit var tvConfirm: CgTextView
    private lateinit var editContent: AppCompatEditText
    private lateinit var tvContentSize: CgTextView
    private lateinit var ivAdd: AppCompatImageView
    private lateinit var tagFlowLayout: TagFlowLayout
    private lateinit var tvPrompt: CgTextView

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_add_tag
    }

    override fun initPopupContent() {
        super.initPopupContent()
        tvPrompt = findViewById(R.id.tv_prompt)
        tagFlowLayout = findViewById(R.id.flowLayout_recent)
        ivAdd = findViewById(R.id.iv_add)
        tvContentSize = findViewById(R.id.tv_content_size)
        tvConfirm = findViewById(R.id.tv_confirm)
        editContent = findViewById(R.id.edit_content)

        tvPrompt.text = "已添加${list.size}个标签（最多添加10个）"

        editContent.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                tvContentSize.text = "${(s?.length ?: 0) + 1}/16"
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

        val searchTagAdapter = object : TagAdapter<String>(list) {
            override fun getView(parent: FlowLayout, position: Int, s: String?): View {
                val flowLayout = LayoutInflater.from(context).inflate(
                    R.layout.app_flow_text,
                    tagFlowLayout, false
                )
                val tv1 = flowLayout.findViewById<CgTextView>(R.id.item_tv_1)
                val tvContent = flowLayout.findViewById<CgTextView>(R.id.item_tv_2)
                tv1.visibility = View.GONE
                tvContent.text = s
                return flowLayout
            }
        }

        tagFlowLayout.adapter = searchTagAdapter
        tvConfirm.setOnClickListener {
            confirmClickListener.invoke(list)
        }

        ivAdd.setOnClickListener {
            if (TextUtils.isEmpty(editContent.text.toString())) {
                ToastUtils.showShort("请输入标签内容")
                return@setOnClickListener
            }
            if(list.size>10){
                ToastUtils.showShort("最多添加10个标签")
                return@setOnClickListener
            }
            list.add(editContent.text.toString())
            searchTagAdapter.notifyDataChanged()
        }
    }

    fun setConfirmClickListener(click: (id: ArrayList<String>) -> Unit): AddTagPop {
        confirmClickListener = click
        return this
    }
}